package com.cloud.tsp.config;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 杨斌
 * 2025-03-2025-03-14
 */

public class DateUtils {

    public static String toChineseDate(String date) {
        // 正则表达式适配 "2025-12-31" 格式
        Pattern pattern = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
        Matcher matcher = pattern.matcher(date);
        if (!matcher.find()) {
            throw new IllegalArgumentException("无效的日期格式，正确格式应为：yyyy-MM-dd");
        }

        String year = matcher.group(1);
        int month = Integer.parseInt(matcher.group(2));
        int day = Integer.parseInt(matcher.group(3));

        String yearInChinese = convertYear(year) + "年";
        String monthInChinese = convertMonth(month) + "月";
        String dayInChinese = convertDay(day) + "日";

        return yearInChinese + monthInChinese + dayInChinese;
    }

    // 以下方法保持不变（同原始代码）
    private static String convertYear(String year) {
        StringBuilder sb = new StringBuilder();
        for (char c : year.toCharArray()) {
            sb.append(digitToChinese(c));
        }
        return sb.toString();
    }

    private static String convertMonth(int month) {
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("月份无效：" + month);
        }
        switch (month) {
            case 10: return "十";
            case 11: return "十一";
            case 12: return "十二";
            default: return digitToChinese((char) ('0' + month));
        }
    }

    private static String convertDay(int day) {
        if (day < 1 || day > 31) {
            throw new IllegalArgumentException("日期无效：" + day);
        }
        if (day >= 30) {
            return day == 30 ? "三十" : "三十一";
        }
        if (day >= 20) {
            int remainder = day - 20;
            return remainder == 0 ? "二十" : "二十" + digitToChinese((char) ('0' + remainder));
        }
        if (day >= 10) {
            int remainder = day - 10;
            return remainder == 0 ? "十" : "十" + digitToChinese((char) ('0' + remainder));
        }
        return digitToChinese((char) ('0' + day));
    }

    private static String digitToChinese(char c) {
        switch (c) {
            case '0': return "零";
            case '1': return "一";
            case '2': return "二";
            case '3': return "三";
            case '4': return "四";
            case '5': return "五";
            case '6': return "六";
            case '7': return "七";
            case '8': return "八";
            case '9': return "九";
            default:  throw new IllegalArgumentException("无效的数字字符：" + c);
        }
    }

    // 新增的方法，用于将字符串日期转换为指定格式
    public static String formatDateString(String dateStr, String inputPattern, String outputPattern) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputPattern);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputPattern);
        LocalDate date = LocalDate.parse(dateStr, inputFormatter);
        return date.format(outputFormatter);
    }

    // 新增的方法，用于将字符串日期转换为 LocalDate 类型
    public static LocalDate parseStringToLocalDate(String dateStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(dateStr, formatter);
    }

    public static String formatDateToString(LocalDate date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return date.format(formatter);
    }
}

